<?php $this->load->view(TEMPLATE_NAME.'/admin/header');?>
<?php $this->load->view(TEMPLATE_NAME.'/admin/side_left');?>
<script type="text/javascript" src="<?php echo base_url()?>js/jquery-1.4.2.js"></script>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>

<script type="text/javascript">
  $(document).ready(function(){
    // initialization on onload
    var map;
    var marker;
    var border;
    initialize();
    $('#sys_city_address').change(function(){
      codeAddress();
    });
    $('#sys_street_address').change(function(){
      codeAddress();
    });
        
    $('#sys_region').change(function(){
      codeAddress();
    });

  });    
  function initialize() {
    geocoder = new google.maps.Geocoder();
<?php if($latitude == '' || $longitude == ''): ?>
    var myLatlng = new google.maps.LatLng(-33.92526381, 18.420553054565435);
      $('#sys_lat').val('-33.92526381');
      $('#sys_lng').val('18.420553054565435');
<?php else: ?>
	var myLatlng = new google.maps.LatLng(<?php echo $latitude ?>, <?php echo $longitude ?>);
      $('#sys_lat').val('<?php echo $latitude ?>');
      $('#sys_lng').val('<?php echo $longitude ?>');
<?php endif; ?>
    var myOptions = {
      zoom: 15,
      center: myLatlng,
      mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
   var image = new google.maps.MarkerImage('<?php echo base_url(); ?>image/point/pin_point.png',
      // This marker is 20 pixels wide by 32 pixels tall.
      new google.maps.Size(24, 35),
      // The origin for this image is 0,0.
      new google.maps.Point(0,0),
      // The anchor for this image is the base of the flagpole at 0,32.
      new google.maps.Point(0, 32));
  var shadow = new google.maps.MarkerImage('<?php echo base_url(); ?>image/point/pin_point_shadow.png',
      // The shadow image is larger in the horizontal dimension
      // while the position and offset are the same as for the main image.
      new google.maps.Size(49, 35),
      new google.maps.Point(0,0),
      new google.maps.Point(10, 35)); 
    marker = new google.maps.Marker({
        position: myLatlng, 
        map: map,
	draggable: true,
	title:"Incident Location",
	icon: '<?php echo base_url(); ?>image/point/pin_point.png',
	shadow: shadow  
    });   
      $('#sys_latlng').val(myLatlng);
    // Add dragging event listeners.
    google.maps.event.addListener(marker, 'dragend', function() {
      $('#sys_latlng').val(marker.getPosition());
      $('#sys_lat').val(marker.getPosition().lat());
      $('#sys_lng').val(marker.getPosition().lng());
    });
    <?php if((!empty($radius)) && (!empty($radius_type))){?>
    drawCircle(map,myLatlng,<?php echo $radius ?>,'<?php echo $radius_type ?>');
    <?php } ?>

  }
    function codeAddress() {
      var street_address = document.getElementById("sys_street_address").value;
      var city_address = document.getElementById("sys_city_address").value;
      var region = $('#sys_region :selected').text();
      var address = street_address+', '+city_address+', '+region;
      //alert(address);
      if (geocoder) {
	geocoder.geocode( { 'address': address}, function(results, status) {
	  if (status == google.maps.GeocoderStatus.OK) {
	    map.setCenter(results[0].geometry.location);
	    marker.setPosition(results[0].geometry.location);
	    $('#sys_latlng').val(marker.getPosition());
	    $('#sys_lat').val(marker.getPosition().lat());
	    $('#sys_lng').val(marker.getPosition().lng());
	  } else {
	    //alert("Geocode was not successful for the following reason: " + status);
	  }
	});
      }
    }
			function drawCircle(map, point, radius, unit) {
				var d2r = Math.PI / 180;
				var circleLatLngs = new Array();
				if(unit == 'km'){
					var circleLat = radius * 0.014483 / 1.609344 ;  // Convert statute km into degrees latitude
				}else{
					var circleLat = radius * 0.014483;  // Convert statute miles into degrees latitude
				}
				var circleLng = circleLat / Math.cos(point.lat() * d2r);
				var numPoints = 100;
 
				var latlngbounds = new google.maps.LatLngBounds( );
 
				// 2PI = 360 degrees, +1 so that the end points meet
				for (var i = 0; i < numPoints + 1; i++) { 
					var theta = Math.PI * (i / (numPoints / 2)); 
					var vertexLat = point.lat() + (circleLat * Math.sin(theta)); 
					var vertexLng = parseFloat(point.lng()) + parseFloat((circleLng * Math.cos(theta)));
					var vertextLatLng = new google.maps.LatLng(vertexLat, vertexLng);
					circleLatLngs.push(vertextLatLng);
					latlngbounds.extend(vertextLatLng);
				}
 
				border = new google.maps.Polygon({
					paths: circleLatLngs
				});
 
				border.setMap(map);
				map.fitBounds(latlngbounds);
			}

</script>
<style media="screen" type="text/css">
#map {
    height: 500px;
    width: 590px;
    margin: 0;
    padding: 0;
    border: 0;
}
</style>
<div class="page noright adminpage adminuser">
        
    <?php $this->load->view(TEMPLATE_NAME.'/admin/profile/profile_tabs');?>
    <h2 class="no">Your Location</h2>
    <p class="black f13"><strong>Turn it Around uses a map based system to help communities tackle crime in their area.</strong><br />
    <span class="f12">In order to bring your relevant news and crime alerts, we need to know your physical location.<br />
    This information is <strong>treated with the strictest confidentiality and will never be shared or  disclosed under any circumstances.</strong></span></p>
    <hr />
    <p class="black f13"><strong>Input and confirm your location:</strong><br /> 
    <span class="f12">Your address will be used as the centerpoint for the Crime Aware alerts and will be used as your default starting point when reporting crime.</span></p>
<?php if(!empty($notification)): ?>
<p><?php echo $notification ?></p>
<?php endif; ?>

    <hr />        
        <?php echo form_open('admin/profile/location',array('method' => 'post', 'class' => 'formstep formp', 'id' => 'location_form'));?><br />
        <div class="fright">
        <p>
        <strong>Please take care to ensure that the address indicated on the map is the correct address, in the correct area.</strong>
        <br /><br />
        <strong>Capitals:</strong> Use capitals when inputting your address e.g. <strong>123</strong> <strong>M</strong>ain <strong>S</strong>treet, 
        <strong>B</strong>edfordview.
        <br /><br />
        <strong>Street/Road/Avenue:</strong> Sometimes Google maps may have your address saved differently as a Street instead of an Avenue or Road e.g. Dlamini Avenue vs Dlamini Street.
        <br /><br />
        <strong>City or Town:</strong> Please include this in the search to reduce the probability of mistakes occurring. 
        </p>
        <p>
        <strong>REPORT CIRCLE</strong><br />
        Please select the radius around your location within which you would like to receive automatic reports and alerts. Simply select from the options below and click submit to save. The black circle on the map indicates the chosen area.<br />
        <strong>Radius</strong> 
        <select id="radius" name="radius">            
            <?php for ($i = 1; $i <= 5; $i++){   ?>
                <option value="<?php echo $i?>" <?php if($i == $radius){echo 'selected="selected"';}?> >
                <?php echo $i?>
                </option>
            <?php }?>
        </select>  

        <select id="type" name="type">
            <option value="km" <?php if($radius_type == "km"){echo 'selected="selected"';}?>>Kilometers</option>
            <option value="miles" <?php if($radius_type == "miles"){echo 'selected="selected"';}?>>Miles</option>            
        </select>
        </p>
        </div><!-- fright -->
       
<hr/>
<strong>Street Address: <sup>*</sup></strong><br/>

<?php 
$street_address_input = array(
	'id'=>'sys_street_address',
	'name'=>'address'
	);
echo form_input($street_address_input,$address);?>
<br/>
<?php echo form_error('address','<label id="warning" class="alert"><strong>Error:</strong>','</label>') ?>
<br/>
<strong>City/Town: <sup>*</sup></strong><br/>
<?php 

$city_address_input = array(
	'id'=>'sys_city_address',
	'name'=>'city'
	);
echo form_input($city_address_input,$city);?><br/>
<?php echo form_error('city','<label id="warning" class="alert"><strong>Error:</strong>','</label>') ?>
<?php
$CI = get_instance();
$CI->load->model('Location_model');
$CI->load->model('Crime_type_model');
$region_list=$CI->Location_model->get_region_by_country(224);
 ?>
<br/>
<strong>Province: <sup>*</sup></strong>
<br/>
<select name="region_id" id="sys_region">
<option>Select Province</option>
<?php if(!empty($region_list)): ?>
<?php foreach($region_list as $key=>$value): ?>

<option <?php if($value['id'] == $region_id){ ?>selected="selected"<?php } ?> value="<?php echo $value['id']?>"><?php echo $value['name'] ?></option>
<?php endforeach;?>
<?php endif; ?>
</select>

<br/>
<br />
        <div id="map_canvas" style="width:330px;height:301px">
        </div>
<br/>
<div click="f12">
<strong>Confirm the pin is placed correctly.</strong> Once the address is found, drag the pin to the exact location of the incident.<br />
Once you are happy that you have selected the location accurately, simply click SUBMIT.
</div>

<!--
        <strong class="f14"><a> &gt; Click on the map to put marker location </a></strong><br />
-->
        <!--<span class="f12">(use this is the marker above is not accurate)</span>-->
        <!--<label id="warning" class="alert"></label>-->
        <div class="clear"></div>
        <hr />
        <input type="hidden" name="latitude" id="sys_lat" value="<?php echo $latitude?>">
        <input type="hidden" name="longitude" id="sys_lng" value="<?php echo $longitude?>">
        <?php 
        $form_button = array(
            'type'=>'submit',
            'name'=>'submit',
            'value'=>'submit',
            'content'=>'Submit',
        );
        echo form_button($form_button) ?>
        <div class="clear"></div>
        </form><!-- formstep formp-->    
    <div class="clear"></div>
</div><!-- page -->
<?php $this->load->view(TEMPLATE_NAME.'/admin/footer');?>
